package com.zzyweb.demomp.controller;

import com.zzyweb.demomp.entity.User;
import com.zzyweb.demomp.entity.ViewObject.UserVO;
import com.zzyweb.demomp.service.UserService;
import com.zzyweb.demomp.utils.JWTUtil;
import com.zzyweb.demomp.utils.ResultObject;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhiyi
 * @date 9/9/25
 **/
@RestController
@RequestMapping("/userspace")
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/logIn")
    public ResultObject logIn(@RequestParam String username, @RequestParam String password, HttpServletResponse response) {
        //登陆检验
        User user = userService.login(username, password);
        if (user == null){
            return ResultObject.error().msg("账号或者密码错误");
        }
        //创建token 传给响应
        String accessToken = JWTUtil.createAccessToken(user);
        response.setHeader("accessToken", accessToken);
        response.setHeader("Access-Control-Expose-Headers","token");

        //创建view object 视图对象-避免暴露全部信息
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);

        return ResultObject.ok(userVO);
    }

}
